<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<?php

/**
 * Created by PhpStorm.
 * User: dapeng
 * Date: 2018/9/27
 * Time: 上午9:47
 */


try{
    //已经完成了连接数据库
    $pdo = new PDO("mysql:dbname=test0926;host=127.0.0.1","root","");
//    echo "连接成功";//没有错误,代表成功
    //连接后马上改变编码
    $pdo->exec("SET NAMES 'utf8';");

//    $sql = "select * from tb_comment c left join tb_user u ON c.user_id = u.id;";

    $sql = "SELECT *,u.id uid,c.id cid FROM tb_comment c LEFT JOIN tb_user u ON c.user_id = u.id;";
    //查到pdo对象结果集
    $res = $pdo->query($sql);
    //一行一行的循环
    echo "<table border='1'>";

    echo "<tr>
            <th>编号</th>
            <th>名字</th>
            <th>评论内容</th>
            <th>评论时间</th>
            <th>操作栏</th>
          </tr>";
    foreach ($res as $row){
//        var_dump($row);
        echo "<tr>";
        //如果只要单个字段
        echo "<td>{$row['cid']}</td>";
        echo "<td>{$row['name']}</td>";
//        echo "<td>{$row['pass']}</td>";
        echo "<td>{$row['text']}</td>";
        echo "<td>{$row['time']}</td>";
        echo "<td>
    <a href='updateHTML.php?id={$row['cid']}&text={$row['text']}'>修改</a>
    <a href='delete.php?id={$row['cid']}'>删除</a></td>";
        echo "</tr>";
    }
    echo "</table>";

    //作业,
//    1、查询评论表,并显示出用户的信息(连接查询),要用表格显示
//    2、增加一条"用户"进入数据库;
//    3、通过html的form表单填写数据,提交的数据,再存入数据库(exec);





}catch (Exception $e){
    echo "连接失败".$e->getMessage();
}

